package com.user.servlet;

import com.user.bean.indexDisscuss;
import com.user.bean.indexUser;
import com.user.dao.indexDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;


public class indexDiscussServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String message = request.getParameter("message");
        HttpSession session=request.getSession();
        indexUser user= (indexUser) session.getAttribute("user");
        String userName=user.getUsername();
        indexDisscuss disscuss=new indexDisscuss();
        disscuss.setTime();
        disscuss.setUserName(userName);
        disscuss.setMessage(message);

        new indexDao().addDiscuss(disscuss);
//        response.sendRedirect(request.getContextPath()+"/index.jsp");
        String re=request.getContextPath()+"/index.jsp";
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>alert('留言成功!')</script>");
//        response.sendRedirect(request.getContextPath()+"/register.jsp");
        out.println("<script language='javascript'>window.location.href='"+re+"'</script>");


    }
}
